/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.util;

import java.io.IOException;
import org.apache.http.HttpStatus;
import org.apache.http.HttpException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;

/**
 * @author 袁凤建
 * <br />邮箱：yuan.fengjian@ustcinfo.com
 * <br />描述：HttpClientUtil.java
 * <br />版本: 1.0.0
 * <br />日期：2013-8-6 下午3:25:29
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

public class HttpClientUtil {
	
	/**
	 * 默认构造方法.
	 */
	public HttpClientUtil() {
		
	}
	
	public static String get(String url) throws HttpException {
		HttpClient httpClient = new HttpClient();
//		httpClient.getHostConfiguration().setProxy("proxy.ah.cmcc", 8080);
//		httpClient.getParams().setAuthenticationPreemptive(true);
		//创建POST方法的实例
		PostMethod postMethod = new PostMethod(url);
		//使用系统提供的默认的恢复策略
		postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
		String result = null; //初始化返回结果(String类型)
		byte[] responseBody = null; //初始化返回结果(byte[]类型)
		try {
			//执行getMethod
			int statusCode = httpClient.executeMethod(postMethod);
		    if (statusCode != HttpStatus.SC_OK) {
			    System.err.println("Method failed: " + postMethod.getStatusLine());
		    }
		    //返回结果(byte[]类型) 
		    responseBody = postMethod.getResponseBody();
		    //返回结果(String类型,GBK格式)
		    result = new String(responseBody, "GBK");
		} catch (IOException e) {
		   e.printStackTrace();
		} finally {
			//释放连接
			postMethod.releaseConnection();
		}
		return result;
	}
}